package com.hn.shopadmin.controller;

import com.hn.shopadmin.util.JwtTokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class BashController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    /**
     * 得到request对象
     */
    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    /**
     * 获取token
     * @return
     */
    public String getToken(){
        HttpServletRequest request = getRequest();
        return request.getHeader(tokenHeader);
    }

    /**
     * 获取登录用户的手机号
     * @return
     */
    public String getUserName(){
        String token = getToken();
        return jwtTokenUtil.getUserNameFromToken(token);
    }

}
